<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Cola\Mapper;

use App\Cola\Model\ColaUser;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Mine\Abstracts\AbstractMapper;

/**
 * 可乐记账-用户表Mapper类
 */
class ColaUserMapper extends AbstractMapper
{
    /**
     * @var ColaUser
     */
    public $model;

    public function assignModel()
    {
        $this->model = ColaUser::class;
    }

    /**
     * 通过用户名检查用户.
     * @return Builder|Model
     */
    public function checkUserByMobile(string $mobile)
    {
        return $this->model::query()->where('mobile', $mobile)->firstOrFail();
    }

    /**
     * 通过用户名检查是否存在.
     */
    public function existsByMobile(string $mobile): bool
    {
        return $this->model::query()->where('mobile', $mobile)->exists();
    }

    /**
     * 通过邮箱检查用户.
     * @return Builder|Model
     */
    public function checkUserByEmail(string $email)
    {
        return $this->model::query()->where('email', $email)->firstOrFail();
    }

    protected function isEmail($value)
    {
        $validator = container()->get(ValidatorFactoryInterface::class)->make(
            ['account' => $value],
            ['account' => 'email']
        );
        return !$validator->fails();
    }

    protected function isMobile($value)
    {
        return preg_match('/^1\d{10}$/', $value);
    }
    protected function isWeChatOpenId($value)
    {
        // 假设微信 OpenID 为长度在28位的字母数字
        return preg_match('/^[a-zA-Z0-9_-]{28}$/', $value);
    }

    /**
     * 通过邮箱检查用户.
     * @return Builder|Model
     */
    public function checkUserByAccount(string $account)
    {
        $field = '';
        if ($this->isEmail($account)){
            $field = 'email';
        }elseif($this->isMobile($account)){
            $field = 'mobile';
        }elseif($this->isWeChatOpenId($account)){
            $field = 'wechat_openid';
        }
        return $this->model::query()->where($field, $account)->firstOrFail();
    }


    /**
     * 通过邮箱检查是否存在.
     */
    public function existsByEmail(string $email): bool
    {
        return $this->model::query()->where('email', $email)->exists();
    }

    /**
     * 检查用户密码
     */
    public function checkPass(string $password, string $hash): bool
    {
        return $this->model::passwordVerify($password, $hash);
    }



    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 组别ID
        if (isset($params['group_id']) && filled($params['group_id'])) {
            $query->where('group_id', '=', $params['group_id']);
        }

        // 昵称
        if (isset($params['nickname']) && filled($params['nickname'])) {
            $query->where('nickname', 'like', '%'.$params['nickname'].'%');
        }

        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 电子邮箱
        if (isset($params['email']) && filled($params['email'])) {
            $query->where('email', 'like', '%'.$params['email'].'%');
        }

        // 手机号
        if (isset($params['mobile']) && filled($params['mobile'])) {
            $query->where('mobile', 'like', '%'.$params['mobile'].'%');
        }

        // 性别
        if (isset($params['gender']) && filled($params['gender'])) {
            $query->where('gender', '=', $params['gender']);
        }

        // 生日
        if (isset($params['birthday']) && filled($params['birthday']) && is_array($params['birthday']) && count($params['birthday']) == 2) {
            $query->whereBetween(
                'birthday',
                [ $params['birthday'][0], $params['birthday'][1] ]
            );
        }


        // 上次登录时间
        if (isset($params['prev_time']) && filled($params['prev_time']) && is_array($params['prev_time']) && count($params['prev_time']) == 2) {
            $query->whereBetween(
                'prev_time',
                [ $params['prev_time'][0], $params['prev_time'][1] ]
            );
        }

        // 登录时间
        if (isset($params['login_time']) && filled($params['login_time']) && is_array($params['login_time']) && count($params['login_time']) == 2) {
            $query->whereBetween(
                'login_time',
                [ $params['login_time'][0], $params['login_time'][1] ]
            );
        }

        // 登录IP
        if (isset($params['login_ip']) && filled($params['login_ip'])) {
            $query->where('login_ip', 'like', '%'.$params['login_ip'].'%');
        }

        // 创建时间
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 更新时间
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 删除时间
        if (isset($params['deleted_at']) && filled($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}